﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CppModel;
using CPPProject.CppServices.Dto;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Linq;
using OplugAbpProject;
using Microsoft.EntityFrameworkCore;

namespace CPPProject.CppServices
{
    public class T_Biz_ItemAppService : AsyncCrudAppService<T_Biz_Item, T_Biz_ItemDto, Guid, PagedResultRequestDto, UpdateT_Biz_ItemDto, UpdateT_Biz_ItemDto>, IT_Biz_ItemAppService
    {
        private readonly IObjectMapper _objectMapper;
        private readonly IT_Biz_ItemConfigHeadAppService _ItemConfigHeadAppService;

        private readonly IRepository<T_Biz_Assembly, Guid> _bizAssembly;

        public T_Biz_ItemAppService(IRepository<T_Biz_Item, Guid> repository, IRepository<T_Biz_Assembly, Guid> bizAssembly, IObjectMapper objectMapper, IT_Biz_ItemConfigHeadAppService itemConfigHeadAppService) : base(repository)
        {
            _objectMapper = objectMapper;
            _ItemConfigHeadAppService = itemConfigHeadAppService;
            _bizAssembly = bizAssembly;
        }
        #region 获取资产信息
        /// <summary>
        /// 获取资产信息
        /// </summary>
        /// <param name="nCompanyID">公司ID</param>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <param name="ItemCode">资产编码</param>
        /// <param name="ItemName">资产名称</param>
        /// <param name="nConfigID">配置ID</param>
        /// <returns></returns>
        public async Task<PagedResultDto<T_Biz_ItemDto>> GetList(string nCompanyID, int skipCount = 0, int maxResultCount = 10, string ItemCode = "", string ItemName = "", string nConfigID = "")
        {
            PagedResultDto<T_Biz_ItemDto> resultDto = new PagedResultDto<T_Biz_ItemDto>();
            List<T_Biz_Item> lists = new List<T_Biz_Item>();
            Expression<Func<T_Biz_Item, bool>> expr = o => o.NCompanyID == nCompanyID;
            if (!string.IsNullOrEmpty(ItemCode))
            {
                expr = expr.ExpressionAnd(o => o.CItemCode == ItemCode);
            }
            if (!string.IsNullOrEmpty(ItemName))
            {
                expr = expr.ExpressionAnd(o => o.CItemName == ItemName);
            }
            if (!string.IsNullOrEmpty(nConfigID))
            {
                var list = await _ItemConfigHeadAppService.GetCname(nConfigID);
                expr = expr.ExpressionAnd(o => o.NConfigID == list.NConfigID);
            }
            lists = await Repository.GetAll().Where(expr).ToListAsync();
            var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
            resultDto.Items = _objectMapper.Map<List<T_Biz_ItemDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion

        #region 资产结构信息
        /// <summary>
        /// 资产结构信息
        /// </summary>
        /// <param name="nCompanyID">公司ID</param>
        /// <param name="CItemCode">资产编码</param>
        /// <returns></returns>
        public async Task<List<ParentDto>> GetAssemblyList(string nCompanyID, string CItemCode = "")
        {
            List<ParentDto> listparent = new List<ParentDto>();
            Expression<Func<T_Biz_Item, bool>> expr = o => o.NCompanyID == nCompanyID && o.ParentId == 0;
            if (!string.IsNullOrEmpty(CItemCode))
            {
                expr = expr.ExpressionAnd(o => o.CItemCode == CItemCode);
            }
            var plist = await Repository.GetAll().Where(expr).ToListAsync();
            if (plist.Count > 0)
            {
                for (int i = 0; i < plist.Count; i++)
                {
                    ParentDto parentDtos = new ParentDto();
                    parentDtos.Totalcount = plist.Count;
                    parentDtos.CItemName = plist[i].CItemName;
                    parentDtos.CItemCode = plist[i].CItemCode;
                    listparent.Add(parentDtos);
                    var list1 = await Repository.GetAll().Where(o => o.CItemCode == plist[i].CItemCode).ToListAsync();
                    if (list1.Count > 0)
                    {
                        parentDtos.ChildDto = await GetAssemblyList(list1[i].NCompanyID, list1[i].CItemCode);
                    }
                }
            }
            return listparent;
        }
        #endregion

    }
}
